package chapter4_1_cas;

import java.util.concurrent.atomic.AtomicLong;

/**
 * 统计0的个数
 * @author huayuhao
 * @date 2020/12/16 16:05
 */
public class Atomic {

    private static AtomicLong atomicLong = new AtomicLong();

    private static Integer[] arr1 = new Integer[]{0, 1, 2, 3, 0, 5, 6, 56, 0};
    private static Integer[] arr2 = new Integer[]{0, 1, 2, 3, 0, 5, 6, 56, 0};

    public static void main(String[] args) throws InterruptedException {

        Thread A = new Thread(() -> {
            int size = arr1.length;
            for(int i = 0; i < size ; i ++){
                if(arr1[i].intValue() == 0){
                    atomicLong.incrementAndGet();
                }
            }
        });

        Thread B = new Thread(() -> {
            int size = arr2.length;
            for(int i = 0; i < size ; i ++){
                if(arr2[i].intValue() == 0){
                    atomicLong.incrementAndGet();
                }
            }
        });

        A.start();
        B.start();
        A.join();
        B.join();
        System.out.println("total count = " + atomicLong.get());
    }

}
